<?php 
$v_url = "/record/handle/index.php?modal_dialog_mode=1" . "&hdn_record_work_id=" . $v_record_work_id . "&fuseaction=DISPLAY_SINGLE_RECORD_WORK_KIEN" ;
$v_recordtype_filter =$_SESSION['onegate_current_recordtype'];
if(isset($_REQUEST['hdn_recordtype_filter'])){
	$v_recordtype_filter = $_REQUEST['hdn_recordtype_filter'];
}
$v_record_work_id = intval($_REQUEST['hdn_item_id']);
$v_record_id = 0;
if(isset($_REQUEST['hdn_record_id'])){
	$v_record_id = intval($_REQUEST['hdn_record_id']);
}
if(isset($_REQUEST['hdn_record_id_list'])){
//	$v_record_id = intval($_REQUEST['hdn_record_id']);
	$v_record_id_list = ($_REQUEST['hdn_record_id_list']);
}
$v_record_id_list = $v_record_id;
//ID nguoi thuc hien cong viec
$v_worker_id = _get_item_attr_by_id($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'id');
$v_worktype=$_REQUEST['hdn_work'];
$v_date= _ddmmyyyy_to_yyyymmdd($_REQUEST['txt_date']);
$v_result =  _replace_bad_char($_REQUEST['txt_result']);
$v_file_id = "";	
//CSDL SQL
if(_is_sqlserver()){
	//echo 'ttttttt :'.$arr_record_list;exit;
	$arr_record_ids = explode(',', $arr_record_list);
	//echo gettype($arr_record_ids).'size :'.sizeof($arr_record_ids); exit;
	
	//nieuvv--111108--update tien do cua cong viec dac biet	
	$arr_rad_work = explode(",",$v_worktype);

	//the-end-nieuvv--111108--
	if($arr_rad_work[2] == "SPEC")		// neu la cong viec dac biet
	{
		var_dump($arr_record_ids);
		for($j=0; $j < sizeof($arr_record_ids); $j++)
		{		
			// lay ham tuong ung clone_time hay release_time
			$sql = "Exec Onegate_GetSpecTaskStart ";
			$sql = $sql . "'".$arr_rad_work[0]."'";
			$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
			$arr_spec_task_start = $ado_conn->GetAll($sql);
			
			$date_process_spec = "";
			// lay ngay thuc hien cong viec dac biet trong tien do
			$sql = "Exec Onegate_GetAllWorkTypeCloneTime ";
			$sql = $sql . "'".$arr_record_ids[$j]."'";
			$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
			$arr_all_process_work = $ado_conn->GetAll($sql); 
			
			// lay ngay thuc hien ngung 
			for($i = 0 ; $i < sizeof($arr_all_process_work) ; $i++)
			{
				$v_clone_date = $arr_all_process_work[$i]['C_DATE'];	// ngay thuc hien trong bang tien do
				$v_process_date = $arr_all_process_work[$i]['C_PROCESS_DATE'];		// so ngay thuc hien ho so
				$v_appointed_date = $arr_all_process_work[$i]['C_APPOINTED_DATE'];
				$v_received_date = $arr_all_process_work[$i]['C_RECEIVED_DATE'];
				if($arr_spec_task_start[0]['C_SPEC_TASK_START'] == "RELEASE_TIME")		
				{
					// goi ham release_time, ngay tiep tuc, ngay hen tra
					$date_process_spec = release_time($v_clone_date,$v_work_date);	// tra ve ngay hen tra
				}
				else if($arr_spec_task_start[0]['C_SPEC_TASK_START'] == "CLONE_TIME")
				{	
					// goi ham clone_time, ngay tiep nhan, ngay hen tra
					$date_process_spec = clone_time(); // tra ve ngay ngung de thuc hien cong viec dac biet
				}
				else
				{
					$date_process_spec = $v_appointed_date;// khong cap nhat vao ngay hen tra
				}
				
				$v_worktype = $arr_rad_work[1];
				$sql = "Exec Onegate_RecordWorkSystemUpdateSpecWorkType " ;
				$sql = $sql . "'" . $arr_record_ids[$j] . "'";
				$sql = $sql . "," . $v_worker_id;//ID nguoi thuc hien cong viec
				$sql = $sql . ",'" . $v_worktype . "'";
				$sql = $sql . "," . $arr_rad_work[0];		
				$sql = $sql . ",'" . $v_date."'";
				$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($date_process_spec)."'";			
				echo $sql;
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs = $ado_conn->GetRow($sql); 
				$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
			}
		}
	}
	else
	{
		//echo 'else';
		for($i=0; $i<sizeof($arr_record_ids); $i++)
		{	
			$v_form_field = 'file_attach';
			if (isset($_FILES[$v_form_field]['tmp_name']))
			{//
				$v_record_work_ = $v_record_work_id;
				if($v_record_work_id==0||$v_record_work_id==''||empty($v_record_work_id))
				{
					$sql = "SELECT MAX(PK_RECORD_WORK) AS NEW_RECORD_WORK FROM dbo.T_ONEGATE_RECORD_WORK";
					$result = mssql_query($sql,$conn);
					$rs = mssql_fetch_array($result);
					$v_record_work_ = $rs['NEW_RECORD_WORK']+1;
				}
				$v_filename = $v_record_id_list.'_'.$v_record_work_.'_work_'._replace_bad_char(trim($_FILES[$v_form_field]['name']));
				$v_tmp_filename = trim($_FILES[$v_form_field]['tmp_name']);
				$v_file_id = 0;
				if ($v_tmp_filename!="" and !is_null($v_tmp_filename))
				{
					_copy_file($v_tmp_filename, _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					if ($v_file_id =="")
					{
						$v_file_id = _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					}
					else
					{			
						$v_file_id = $v_file_id . ",". _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					}	
				}	
			}
			
			$v_result_id = _save_text_to_database('T_TEMP_FILE', 'C_TEXT', $v_result);
			//echo 'tes------111=='.$arr_record_ids[$i] ;exit;
			$sql = "Exec Onegate_RecordWorkUpdate " ;
			$sql = $sql . $v_record_work_id;
			$sql = $sql . "," . $arr_record_ids[$i] ;
			$sql = $sql . "," . $v_worker_id;//ID nguoi thuc hien cong viec
			$sql = $sql . ",'" . $v_worktype . "'";
			$sql = $sql . ",'" . $v_date."'";
			$sql = $sql . "," . $v_result_id ;
			$sql = $sql . "," . $v_file_id;
			$sql = $sql . ",'" . $v_filename."'";
			$sql = $sql . "," . _CONST_STATUS_UPDATE_DATE ;
			$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
			$rs = $ado_conn->GetRow($sql); 
			$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
		}			
	}	
}
//exit;
if (!is_null($v_error) and $v_error<>""){?>
	<script>
		alert("<?php echo $v_error; ?>");
		if (_MODAL_DIALOG_MODE==1){
			window.location = "<?php echo $v_url; ?>";
		}else{
			window.history.back();
		}	
	</script><?php
	exit;
}
sleep(0);
?>
<form action="index.php" method="post" name="f_back">
	<!-- <input type="hidden" name="fuseaction" value=""> -->
	<input type="hidden" name="hdn_record_id"  value="<?php echo $v_record_id; ?>">
	<input type="hidden" name="hdn_recordtype_filter" value="<?php echo $hdn_recordtype_filter;?>">
	<input type="hidden" name="hdn_item_id" value="<?php echo $v_record_work_id;?>">
</form>
<Script language="javascript">
	document.forms(0).submit();
</Script>
